package com.itfitness.calendarview.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.itfitness.calendarview.R
import com.itfitness.calendarview.adapter.CalendarAdapter
import com.itfitness.calendarview.bean.DateBean
import com.itfitness.calendarview.widget.CalendarView
import com.itfitness.calendarview.widget.WrapGridView
import java.util.*
import kotlin.collections.ArrayList

/**
 *
 * @Description:     java类作用描述
 * @Author:         作者名
 * @CreateDate:     2022/2/14 16:03
 * @UpdateUser:     更新者：
 * @UpdateDate:     2022/2/14 16:03
 * @UpdateRemark:   更新说明：
 */
class CalendarFragment(val onDateSelectCallBack:CalendarView.OnDateSelectCallBack): Fragment() {
    private lateinit var mView: View
    private lateinit var wgvCalendar:WrapGridView
    private  var year:Int = 0
    private  var month:Int = 0
    private var calendarAdapter:CalendarAdapter? = null
    @SuppressLint("InflateParams")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mView = inflater.inflate(R.layout.fragment_calendar, null)
        initArguments()
        initView()
        initData()
        return mView
    }

    private fun initArguments() {
        arguments?.let {
            year = it.getInt(YEAR)
            month = it.getInt(MONTH)
        }
    }

    private fun initView() {
        wgvCalendar = mView.findViewById(R.id.wgvCalendar)
        //去掉点击效果
        wgvCalendar.selector = ColorDrawable(Color.TRANSPARENT)
    }

    private fun initData(){
        val data = ArrayList<DateBean>()
        //获取第一天是星期几然后计算出需要填充的空白数据
        repeat(getMonthOneDayWeek() - 1){
            //填充空白的
            data.add(DateBean(0,0,0))
        }
        //填充日期数据
        repeat(getMonthMaxDay()){
            data.add(DateBean(year,month,it + 1))
        }
        calendarAdapter = CalendarAdapter(data,context)
        calendarAdapter?.onDateSelectCallBack = onDateSelectCallBack
        wgvCalendar.adapter = calendarAdapter
    }

    /**
     * 刷新数据
     */
    fun refreshData(){
        calendarAdapter?.notifyDataSetChanged()
    }

    /**
     * 获取第一天为星期几
     */
    private fun getMonthOneDayWeek(): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, year)
        a.set(Calendar.MONTH, month)
        a.set(Calendar.DATE, 1) //把日期设置为当月第一天
        return a.get(Calendar.DAY_OF_WEEK)
    }

    /**
     * 获取当月有几天
     */
    private fun getMonthMaxDay(): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, year)
        a.set(Calendar.MONTH, month)
        return a.getActualMaximum(Calendar.DAY_OF_MONTH)
    }

    companion object{
        const val YEAR = "year" //年
        const val MONTH = "month" //月
    }
}